package cc.rengu.igas.channel.cups.core.service.trans;

import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.igas.channel.cups.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.cups.core.model.CupsFieldRspBean;
import cc.rengu.igas.channel.cups.core.service.base.BasePayService;
import cc.rengu.igas.channel.cups.facade.request.TransferRequest;
import cc.rengu.igas.channel.cups.facade.response.TransferResponse;
import cc.rengu.igas.share.facade.bean.CupsField;
import cc.rengu.igas.share.facade.bean.CupsFieldBean;
import cc.rengu.igas.share.facade.bean.cups.CupsRspCodeEnum;
import cc.rengu.igas.share.facade.bean.cups.F57;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.HexUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * 转账
 *
 * @author Administrator
 */
public class CupsTransferService extends BasePayService<TransferRequest, TransferResponse> {
    @Override
    protected ChnlBizResponse callBizService(CupsFieldBean cupsFieldBean, TransferRequest transferRequest, TransferResponse transferResponse) throws Exception {
        CupsField cupsField = cupsFieldBean.getCupsField();

        String tmpAmt = AmountUtil.changeY2F(transferRequest.getOrderAmt());
        cupsField.setOrderAmt(String.format("%012d", Integer.valueOf(tmpAmt)));
        cupsField.setMsgType("0200");
        cupsField.setProcCode("400000");
        cupsField.setConCode("00");

        /**104域赋值*/
        if (transferRequest.getTfrAcctFlag() != null) {
            /*TT用法*/
            cupsField.setTermAttachData(HexUtil.StringtoHex("TT") + "303033" + "0101" + HexUtil.StringtoHex(transferRequest.getTfrAcctFlag()));
        }

        cupsField.setLocalDate(transferRequest.getTransDateTime().substring(0, 4));
        cupsField.setLocalTime(transferRequest.getTransDateTime().substring(4));
        cupsField.setReferenceNo(cupsField.getLocalTime() + cupsField.getCupsTraceNo());

        //  终端读取能力
        //  IC卡条件代码
        //  小额免密标志
        //  终端类型
        //  特殊计费类型
        cupsFieldBean.createF60()
                .setF60_2_2(transferRequest.getTermRead())
                .setF60_2_3(transferRequest.getIcConCode())
                .setF60_2_6(transferRequest.getNoPinFlag())
                .setF60_2_5(transferRequest.getTermType())
                .setF60_3_1(transferRequest.getSpecialBillType());
        cupsFieldBean.build();

        //  调用 CUPS   TODO 此交易配置暂时有误 ,关联了行内交易码, 后续解耦
        CupsFieldRspBean cupsFieldRspBean = callCups(cupsFieldBean,
                transferRequest.getHeader().getTxnNum(), CupsParamConstant.CUPS_8583_MAC_TYPE_TA);

        CupsField cupsFieldRsp = cupsFieldRspBean.getCupsFieldRsp();
        BeanUtil.beanCopy(cupsFieldRsp, transferResponse);
        if (transferResponse.getSettleDate() != null) {
            transferResponse.setSettleDate(getCupsSettleDate(transferResponse.getSettleDate()));
        }

        transferResponse.setIcData(cupsFieldRsp.getIcData());
        if (CupsParamConstant.SUCCESS_CODE.equals(cupsFieldRsp.getRespCode())) {
            transferResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            transferResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
        } else {
            if (!StringUtil.isEmptyOrNull(cupsFieldRsp.getAddData())) {
                F57 f57 = new F57();
                f57 = f57.parseF57_AS(cupsFieldRsp.getAddData());
                if (null != f57) {
                    transferResponse.setAddDataRspCode(f57.getF57_AS_AR_2());
                }
            }
            transferResponse.setRespCode(cupsFieldRsp.getRespCode());
            transferResponse.setRespDesc(CupsRspCodeEnum.getRspMsg(cupsFieldRsp.getRespCode(), transferResponse.getAddDataRspCode()));
        }
        return new ChnlBizResponse(transferResponse);
    }
}
